<?php
class Web_View_Helper_CommerceTags
	extends Zend_View_Helper_Abstract 
{
	const INVALID_PARAMS = 'Invalid params in Web_View_Helper_CommerceTags';
    protected $_defaults = array(
        'link' => true,
        'label' => true
    );
    
    protected $_options;
    
//	public function commerceTags( $tags, $type, $negocio = null, $link = true ) 
	public function commerceTags( $options ) 
	{
	    
	    //tags, type, negocio, link, label
	    $this->_options = array_merge( $this->_defaults, $options);
	    
		switch ( $this->_options['type'] ){
		
			case 'food':
				return $this->_food();
				
			case 'district':
				return $this->_district();
				
			case 'payment':
				return $this->_payment();
		    
			default:
                throw new Zend_View_Exception( self::INVALID_PARAMS );
				
		}
		
	}
	
	protected function _district( )
	{
	    $links = array( );
	    
	    $negocio = isset( $this->_options['negocio'] ) ? $this->_options['negocio'] :  false;
	    
	    if ( $negocio ){
	        $ciudad = $negocio->getCiudad();
	    }
	    
	    foreach($this->_options['tags'] as $tag){
					
			$tagName = $this->view->varUrl( $tag->nombre );
			
    	    if ( $negocio ){
    	        
    	        $params = array(
    	        	'tagName'   => $tagName,
    	        	'ciudad'    => $this->view->varUrl( $ciudad->nombre ),
    	        	'provincia' => $this->view->varUrl( $ciudad->provincia )
    	        );
    	        
    	        if ( $this->_options['link'] ){
    	            $links[] = "<a href='". $this->view->url($params, 'ciudad-tag')."'>{$tag->nombre}</a>";
    	        } else {
    	            $links[] = "<span>{$tag->nombre}</span>";
    	        }
    	        
    	    } else {
    	        
    	        if ( $this->_options['link'] ){
    	            $links[] = "<a href='". $this->view->url(array('tagName'=>$tagName), 'barrio')."'>{$tag->nombre}</a>";
    	        } else {
    	            $links[] = "<span>{$tag->nombre}</span>";
    	        }
    	        
    	    }
		}
		
		if ( $this->_options['label'] ){
		    $label = $this->view->translate('shipping') . ': ';
		} else {
		    $label = null;
		}
		
		return "<p class='tag-district'>{$label}<span>" . implode(", ", $links) . "</span></p>";
	}
	
    protected function _food()
	{
	    $links = array( );
	    
	    foreach($this->_options['tags'] as $tag){
					
			$tagName = $this->view->varUrl( $tag->nombre );
			
	        if ( $this->_options['link'] ){
	            $links[] = "<a href='". $this->view->url(array('tagName'=>$tagName), 'comida')."'>{$tag->nombre}</a>";
	        } else {
	            $links[] = "<span>{$tag->nombre}</span>";
	        }
			
		}
	    
	    return "<p class='tag-food'>" . implode(", ", $links) . "</p>";
	}
	
    protected function _payment( )
	{
	    $links = array( );
	    
	    foreach( $this->_options['tags'] as $tag){
					
			$tagName = $this->view->varUrl( $tag->nombre );
			
			$n = str_replace(" ", "", $tag->nombre);
			
			$links[] = "<li class='payment{$n}'></li>";
		}
		
		return "<ul class='tag-payment'>" . implode(null, $links) . "</ul>";
	}
	

}